<?php

namespace App\Models\Oauth;

use Illuminate\Support\Facades\DB;
use App\Models\Model;

/**
 * 返回的授权Code模型
 */
class CodeModel extends Model
{

    const TABLENAME = 'tw_passport_code';

    const APP_STATUS_BAN = 1; //禁用
    const APP_STATUS_NOL = 0; //正常

    /**
     * 初始化并返回数据库请求构建器
     *
     * @return Illuminate\Support\Facades\DB::table(TABLENAME)
     */
    public static function dbTable()
    {
        return DB::table(self::TABLENAME);
    }

    public static function addCode($code, $codeData, $expire)
    {
        $data['code'] = $code;
        $data['code_data'] = serialize($codeData);
        $data['expire'] = $expire;
        return self::dbTable()->insert($data);
    }

    public static function getCode($code)
    {
        $where[] = ['code', '=', $code];
        $where[] = ['expire', '>', time()];
        $data = self::dbTable()->where($where)->first();
        if($data){
           return unserialize($data['code_data']);
        }
        return $data;
    }

}

?>